﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.ComponentModel;

namespace MoneyWentWhere.Model
{
    [DebuggerDisplay("Tx {Date}")]
    public class Transaction : INotifyPropertyChanged
    {
        // Stored fields
        Guid _id;
        DateTime _date;
        decimal _amount;
        String _comment;
        String _category;

        // Runtime fields
        Account _account;
        Guid _reconcileId;
        Guid _transferId;

        public Transaction()
        {
            _id = Guid.NewGuid();
        }

        public Guid Id { get { return _id; } }

        public Account Account { get { return _account; } }
        public void SetAccount(Account acct)
        {
            if (_account != null) { throw new InvalidOperationException("Account can only be set once."); }
            _account = acct;
        }

        // Variable items
        
        // Note: setting the date changes position in the list
        public DateTime Date 
        {
            get { return _date; }
            set
            {
                _date = value;
                OnPropertyChanged("Date");
            }
        }

        public decimal Amount 
        { 
            get { return _amount; }
            set 
            {
                _amount = value;
                OnPropertyChanged("Amount");
            }
        }

        public String Comment 
        {
            get { return _comment; }
            set
            {
                _comment = value;
                OnPropertyChanged("Comment");
            }
        }

        public String Category 
        {
            get { return _category; }
            set
            {
                _category = value;
                OnPropertyChanged("Category");
            }
        }

        public Guid ReconcileId
        {
            get { return _reconcileId; }
            set
            {
                _reconcileId = value;
                OnPropertyChanged("ReconcileId");
            }
        }

        public Guid TransferId 
        {
            get { return _transferId; }
            set
            {
                _transferId = value;
                OnPropertyChanged("TransferId");
            }
        }


        
        

        void OnPropertyChanged(String propertyName)
        {
            if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
